--if on server
if SERVER then

	--add the file
	AddCSLuaFile("shared.lua");

	--set the hold type
	SWEP.HoldType			= "pistol";

end

SWEP.Base = "ts2_base";
 
	--set some stuff
	SWEP.PrintName			= "Overwatch Combat Pistol";
	SWEP.TS2Desc		= "(UNHOLSTERED)Left Click: Fire\n(HOLSTERED) Right Click: Change fire mode\n";

--is it spawnable?
SWEP.Spawnable				= false;
SWEP.AdminSpawnable			= false;

--models
SWEP.ViewModel				= "models/weapons/v_alyxgun.mdl";
SWEP.WorldModel				= "models/weapons/W_Alyx_Gun.mdl";

--primary settings
SWEP.Primary =
{
	ClipSize				= 25,
	DefaultClip				= 25,
	Automatic				= false,
	Ammo					= "pistol"
}

--secondary settings
SWEP.Secondary =
{
	ClipSize				= -1,
	DefaultClip				= -1,
	Automatic				= false,
	Ammo					= "none"
}

SWEP.TS2HoldType = "PISTOL";

 SWEP.Primary.SpreadCone = Vector( .04, .04, .04 );

 SWEP.Primary.IronSightPos = Vector( -5.4, 4.5, -2.0 );
 SWEP.Primary.IronSightAng = Vector( 0.0, 0.0, 0.0 );
 
   
SWEP.Primary.HolsteredPos = Vector( 2.8, -2.0, -2.0 );
SWEP.Primary.HolsteredAng = Vector( -15.0, 15.0, 0.0 );
   
   
SWEP.IconCamPos = Vector( 50, 139, 0 ) 
SWEP.IconLookAt = Vector( 1, 4, -1 ) 
SWEP.IconFOV = 6

--fire mode
SWEP.FireMode = 1;

--on initialize

function SWEP:Precache()
util.PrecacheSound("weapons/alyxgun/fire01.wav")
util.PrecacheSound("weapons/alyxgun/fire02.wav")
util.PrecacheSound("weapons/smg1/smg1_reload.wav")
util.PrecacheSound("weapons/smg1/switch_single.wav")
end

--when we want to attack with the primary
function SWEP:PrimaryAttack()
	if( SERVER ) then
	--if we can
	if self.Weapon:CanPrimaryAttack() then
		
		if self.FireMode == 1 then

			self:FireBullet(Vector(0.02,0.02,0));

					local Sound =
				{
					"weapons/alyxgun/fire01.wav",
					"weapons/alyxgun/fire02.wav"
				}
	
		self.Weapon:EmitSound(Sound[math.random(1,2)])

			--delay the fire
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.14);

		elseif self.FireMode == 2 then

			for I = 1,3 do

				timer.Simple(I * 0.125,
				function()

					if ValidEntity(self.Weapon) && ValidEntity(self.Owner) then
						
						self:FireBullet(Vector(0.045,0.045,0));
						
						if self:CanPrimaryAttack() then
								local Sound =
				{
					"weapons/alyxgun/fire01.wav",
					"weapons/alyxgun/fire02.wav"
				} 

						self.Weapon:EmitSound(Sound[math.random(1,2)])
						end
					end

				end)

			end

			--delay the fire
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.8);

		elseif self.FireMode == 3 then
			
			self:FireBullet(Vector(0.04,0.04,0));
			
					local Sound =
				{
					"weapons/alyxgun/fire01.wav",
					"weapons/alyxgun/fire02.wav"
				} 

			self.Weapon:EmitSound(Sound[math.random(1,2)])

			--delay the fire
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.13);

		end
		
		end

	end

	--return false
	return false;

end

function SWEP:FireBullet(Spread)

	if( CLIENT ) then return; end
	if self:CanPrimaryAttack() then
	--create a bullet
	local Bullet = {};

	--set it up
	Bullet.Src = self.Owner:GetShootPos();
	Bullet.Attacker = self.Owner;
	Bullet.Dir = self.Owner:GetAimVector();
	Bullet.Spread = Spread;
	Bullet.Num = 1;
	Bullet.Damage = 5;
	Bullet.Force = 6;
	Bullet.Tracer = 1;
	Bullet.TracerName = "Tracer";

	--fire it
	self.Weapon:FireBullets(Bullet);
	self.Weapon:ShootEffects();

	--take off some ammo
	self.Weapon:TakePrimaryAmmo(1);
	end
end

function SWEP:SecondaryAttack()

	if( CLIENT ) then return; end

	if( self.Owner:GetPlayerHolstered() ) then 
		self.Weapon:EmitSound(Sound("weapons/smg1/switch_single.wav"))

		self.FireMode = self.FireMode + 1;

		if self.FireMode > 3 then

			self.FireMode = 1;

		end

		if self.FireMode == 3 || self.FireMode == 2 then

			self.Primary.Automatic = true;

		else

			self.Primary.Automatic = false;

		end

		self.Weapon:SetNWInt("smod_FireMode",self.FireMode);

		self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK);

		self.Weapon:SetNextSecondaryFire(CurTime() + 0.5);
	end

end

--when reloading
function SWEP:Reload()

	--reload
	self.Weapon:DefaultReload(ACT_VM_RELOAD);
	if self.Weapon:Clip1() != 25 then
	self.Weapon:EmitSound(Sound("weapons/smg1/smg1_reload.wav"))
	end
	--stop
	return false;

end

if CLIENT then

	function SWEP:DrawHUD()
		--if( self.Owner:GetPlayerHolstered() ) then return; end
			local Mode = self.Weapon:GetNWInt("smod_FireMode",1);
			local Text = "";

			if Mode == 1 then

				Text = "SINGLE";

			elseif Mode == 2 then

				Text = "BURST";

			elseif Mode == 3 then

				Text = "AUTO";

			end

			draw.SimpleText(Text,"default",ScrW() - 25,ScrH() * 0.8,Color(255,0,0,255),TEXT_ALIGN_RIGHT,TEXT_ALIGN_CENTER);
		
	end
end 